<template>
  <el-button type="text" size="mini" :disabled="disabled" @click="handleSetInStock(0)">设置售罄</el-button>
  <el-button type="text" size="mini" :disabled="cancelDisabled" @click="handleSetInStock(1)">设置有货</el-button>
</template>

<script setup>
  import {defineProps, reactive, defineEmits} from 'vue'
  import {ElMessage, ElMessageBox} from 'element-plus'
  import {updateRelGoods} from "@/api/goods/relgoods";

  const props = defineProps({
    row: {
      type: Object,
      required: true
    },
    disabled: {
      type: Boolean,
      default: true
    },
    cancelDisabled: {
      type: Boolean,
      default: true
    },
    mediaid: {
      type: Number,
      required: true
    }
  })

  const formData = reactive({
    mediaid: undefined,
    goodsid: undefined,
    data: {
      stockcount: undefined
    }
  })

  const emit = defineEmits(['reloadGoodsList'])

  const handleSetInStock = (stock) => {
    formData.mediaid = props.mediaid
    formData.goodsid = props.row.goods_id
    formData.data.stockcount = stock
    let title = stock == 1 ? '确定要将商品 ' + props.row.goods_name + ' 设置为有货吗？' : '确定要将商品 ' + props.row.goods_name + ' 设置为售罄吗？'
    ElMessageBox.confirm(title, '', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      updateRelGoods(formData).then(response => {
        if (response.errcode === 0) {
          ElMessage({
            type: 'success',
            message: response.msg,
            duration: 2000,
            onClose: () => {
              emit('reloadGoodsList')
            }
          })
        }
      })
    }).catch(() => {})
  }
</script>

<style scoped>

</style>
